package com.ruoyi.project.module.lyq.controller;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.SysUserRole;
import com.ruoyi.project.domain.YhAppointment;
import com.ruoyi.project.domain.YhAssist;
import com.ruoyi.project.domain.YhInstructor;
import com.ruoyi.project.service.ISysRoleService;
import com.ruoyi.project.service.IYhAppointmentService;
import com.ruoyi.project.service.IYhAssistService;
import com.ruoyi.project.service.IYhInstructorService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import nonapi.io.github.classgraph.json.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.ruoyi.common.security.utils.SecurityUtils.*;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/lyq/appointment")
public class LyqAppointmentController extends BaseController
{
    @Autowired
    private IYhAppointmentService yhAppointmentService;
    @Autowired
    private IYhInstructorService yhInstructorService;
    @Autowired
    private IYhAssistService yhAssistService;
    @Autowired
    private ISysRoleService sysRoleService;
    /**
     * 查询预约信息列表
     */
//    @RequiresPermissions("lyq:appointment:list")
    @GetMapping("/list")
    public TableDataInfo list(YhAppointment yhAppointment)
    {
        startPage();
        System.out.println("yhAppointment1"+yhAppointment.getNote2());
        System.out.println("yhAppointment2"+yhAppointment.getInstrumentId());
        System.out.println("yhAppointment2"+yhAppointment.getAppointmentStart());
        List<YhAppointment> list = yhAppointmentService.selectYhAppointmentList(yhAppointment);

//        for(YhAppointment a:list){
//            a.getAppointmentEnd();
//            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
//            String dateString = formatter.format(a.getAppointmentEnd());
//            System.out.println(dateString);
//        }
        /*SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

        String dateString = formatter.format(yhAppointment.getAppointmentEnd());

        System.out.println(dateString);*/
        return getDataTable(list);
    }

    /**
     * 导出预约信息列表
     */
//    @RequiresPermissions("lyq:appointment:export")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhAppointment yhAppointment)
    {
        List<YhAppointment> list = yhAppointmentService.selectYhAppointmentList(yhAppointment);
        ExcelUtil<YhAppointment> util = new ExcelUtil<YhAppointment>(YhAppointment.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
//    @RequiresPermissions("lyq:appointment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yhAppointmentService.selectYhAppointmentById(id));
    }

    /**
     * 新增预约信息
     */
//    @RequiresPermissions("lyq:appointment:add")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody YhAppointment yhAppointment)
    {
        int  rows =yhAppointmentService.selectThing(yhAppointment);
        if (rows==1) {
            getUsername();
            //查user_role表
//            Long userid=getUserId();
//            SysUserRole sysRole = sysRoleService.selectRoleByUser(userid);
//            Long roleId = sysRole.getRoleId();
            String username = getUsername();
            String instrumentId=yhAppointment.getInstrumentId();
            System.out.println("设备id"+instrumentId);
            LoginUser loginUser = getLoginUser();
            SysUser sysUser = loginUser.getSysUser();
            yhAppointment.setName(sysUser.getNickName());
            yhAppointment.setStudentId(username);
//            yhAppointment.setTeacherResult(String.valueOf(1));
//            YhInstructor yhInstructor = yhInstructorService.selectYhInstructorByStuId(getUsername());
//            if(roleId==4){
//
//                    yhAppointment.setTeacherResult(String.valueOf(1));
//            }

//            else {
//                if (roleId==3&&Objects.isNull(yhInstructor)){
//                    throw new ServiceException("请完善指导教师信息");
//                }
//                else {

                    YhAssist yhAssist=yhAssistService.selectYhAssistByRecId(instrumentId);
                    if (Objects.isNull(yhAssist)){
                        System.out.println("1");
                    }else{
                        yhAppointment.setAssistId(yhAssist.getAssistId());
                        yhAppointment.setAssistName(yhAssist.getAssistName());}

//                    yhAppointment.setName(yhInstructor.getName());

//                }
//            }
        }
        return toAjax(yhAppointmentService.insertYhAppointment(yhAppointment));
    }

    /**
     * 修改预约信息
     */
//    @RequiresPermissions("lyq:appointment:edit")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhAppointment yhAppointment)
    {
        return toAjax(yhAppointmentService.updateYhAppointment(yhAppointment));
    }

    /**
     * 删除预约信息
     */
//    @RequiresPermissions("lyq:appointment:remove")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhAppointmentService.deleteYhAppointmentByIds(ids));
    }

    /**
     * 发送企业微信
     */
    @GetMapping("/message/{receiverId}/{receiverName}/{instrumentName}/{storageLocation}/{appointmentStart}/{appointmentEnd}/{assistment}/{specification}/{instrumentId}")
    public void sendMessage(@PathVariable("receiverId") String receiverId,@PathVariable("receiverName")String receiverName,@PathVariable("instrumentName") String instrumentName,@PathVariable("storageLocation") String storageLocation,@PathVariable("appointmentStart") String appointmentStart,@PathVariable("appointmentEnd") String appointmentEnd,@PathVariable("assistment") String assistment,@PathVariable("specification") String specification,@PathVariable("instrumentId") String instrumentId) {
        String receiverid=receiverId;
        String receivername=receiverName;
        Boolean i= Boolean.valueOf(assistment);
        System.out.println(assistment);

//        Long userid=getUserId();
//        SysUserRole sysRole = sysRoleService.selectRoleByUser(userid);
//        Long roleId = sysRole.getRoleId();


        YhAssist yhAssist=yhAssistService.selectYhAssistByRecId(instrumentId);
        if (Objects.isNull(yhAssist)){
            LyqWeChatMassage.sendWeChatMassage(receiverid,receivername,instrumentName,storageLocation,appointmentStart,appointmentEnd,specification,instrumentId);
            System.out.println(444);
        }else{
            YhAssist yhAssist1=new YhAssist();
            yhAssist1.setNote1(instrumentId);
            List<YhAssist> list = yhAssistService.selectYhAssistList(yhAssist1);
            for (YhAssist l:list)
            {
                String a=l.getAssistId();
                System.out.println("这个是"+a);
                if (a.equals(""))
                {
                    LyqWeChatMassage.sendWeChatMassage(receiverid,receivername,instrumentName,storageLocation,appointmentStart,appointmentEnd,specification,instrumentId);
                    System.out.println(111);
                }
                else {
                    if(i==false){
                        String assisid=yhAssist.getAssistId();
                        String assisname=yhAssist.getAssistName();
                        LyqWeChatMassage.sendWeChatMassage(assisid,assisname,instrumentName,storageLocation,appointmentStart,appointmentEnd,specification,instrumentId);
                        System.out.println(222);
                    }
                    else{
                        String assisid=yhAssist.getAssistId();
                        String assisname=yhAssist.getAssistName();
                        LyqWeChatMassage.sendWeChatMassage(receiverid,receivername,instrumentName,storageLocation,appointmentStart,appointmentEnd,specification,instrumentId);
                        LyqWeChatMassage.sendWeChatMassage(assisid,assisname,instrumentName,storageLocation,appointmentStart,appointmentEnd,specification,instrumentId);
                        System.out.println(333);
                    }
                }
            }



        }






//        if(roleId==3)
//        {
//            YhInstructor yhInstructor=yhInstructorService.selectYhInstructorByStuId(getUsername());
//            String teacherId = yhInstructor.getTeacherId();
//            String teachername=yhInstructor.getTeacherName();
//            LyqWeChatMassage.sendWeChatMassage(teacherId,teachername);}
//        else if(roleId==4){
//            System.out.println("1");
//            System.out.println("3"+receiverid);
//            LyqWeChatMassage.sendWeChatMassage(receiverid,receivername);
//        }

    }
}
